/*
 * ParseGrafo.java
 *
 * 
 */
package it.luigiarpino.alt2jsim.core;


import java.io.File;
import java.io.RandomAccessFile;
import java.util.StringTokenizer;

/**
 *
 * @author  Luigi Lorenzo Arpino 534/559
 */
public class ParseGrafo {
    
    private Grafo grafo;
    private RandomAccessFile fileIn;
    private int numriga=1;
    private String errore;
    
    
    /** Creates a new instance of ParseGrafo */
    public ParseGrafo(String fileToParse_) {
        try{
               File f=new File(fileToParse_);
               if (!f.exists()){
                   System.out.println("File "+fileToParse_+" not found!");
                   System.exit(0);
               }
               fileIn=new RandomAccessFile(f,"r");
        }
        catch(java.lang.Exception e){
            System.out.println("Unable to read "+fileToParse_);
            this.errore=new String("Unable to read "+fileToParse_);
            return;
        }
        try{
            this.ParseFile();
        }
        catch(java.lang.Exception e_){
            System.out.println("\nParsing Error File: "+fileToParse_+" at line: "+this.numriga);
            this.errore=new String("Parsing Error File: "+fileToParse_+"\n at line: "+this.numriga);
            return;
        }
    }
    
    public Grafo getGrafo(){
        return this.grafo;
    }
    
    public String getErrorString(){
        return this.errore;
    }
    
    /** Consuma tutte le righe  inclusa quella che contiene il Token token_*/ 
    private void Consuma(String token_) throws java.lang.Exception{
       StringTokenizer st; 
        do{
            st=new StringTokenizer(fileIn.readLine());
            this.numriga=this.numriga+1;
        }while(st.countTokens()==0 ||!st.nextToken().equalsIgnoreCase(token_));
    }
    
    /** Esegue il parsing di un file.alt*/
    private void ParseFile() throws java.lang.Exception{
        this.Consuma("GRAPH");
        StringTokenizer st=new StringTokenizer(fileIn.readLine());
        this.numriga=this.numriga+1;
        Integer numOfNode=new Integer(st.nextToken());
        Integer numOfArchi=new Integer(st.nextToken());
        StringTokenizer tGrafo=new StringTokenizer(st.nextToken());
        String tipoGrafo=tGrafo.nextToken("(");
        Integer scale=new Integer(st.nextToken());
        grafo =new Grafo(numOfNode.intValue(),numOfArchi.intValue(),scale.intValue(),tipoGrafo);
        // acquisisco i nodi
        this.Consuma("VERTICES");
        st=new StringTokenizer(fileIn.readLine());
        while(st.countTokens()!=0){
            Integer id_nodo=new Integer(st.nextToken());
            String nome_nodo=st.nextToken();
            nome_nodo=nome_nodo.replaceAll("/", "_");
            Integer posX=new Integer (st.nextToken());
            Integer posY=new Integer(st.nextToken());
            this.grafo.setNameOfNode(nome_nodo, id_nodo.intValue());
            this.grafo.setPositionOfNode(id_nodo.intValue(), posX.intValue(), posY.intValue());
            st=new StringTokenizer(fileIn.readLine());
            this.numriga=this.numriga+1;
        }
        // Acquisisco gli archi
        this.Consuma("EDGES");
        st=new StringTokenizer(fileIn.readLine());
        this.numriga=this.numriga+1;
        while(st.countTokens()!=0){
            Integer from_node=new Integer(st.nextToken());
            Integer to_node=new Integer(st.nextToken());
            Integer lenght=new Integer(st.nextToken());
            //String token4=st.nextToken();
            st.nextToken();
            this.grafo.addArch(from_node.intValue(), to_node.intValue(), lenght.intValue());
            try{
                st=new StringTokenizer(fileIn.readLine());
            }
            catch(java.lang.Exception e){
                break;
            }   
        }
    }
    
    
    public static void main(String[] args) {

    }
    
}
